package hyfree.sql;

import java.sql.*;

public class SqlTool {



}

class SqlBase {
    private String userNmae;//用户名
    private String passWord;//密码
    private String driver;//驱动类型，mySql，oracle，sqlServer
    private  String port;//连接的端口
    private  String serverIP;//ip地址  localhost
    private String server;//实例名
    Connection conn;
    Statement st;
    ResultSet rs;
    public SqlBase(){
        //完全缺省的初始化
        this.userNmae="root";
        this.passWord="root";
        this.driver="mySql";
        this.port="3306";
        this.serverIP="localhost";
        this.server="sds";
    }
    public SqlBase(String userNmae,String passWord,String server){
        //缺省的初始化,默认为mySql，端口3306，serverIP=localhost
        this.userNmae=userNmae;
        this.passWord=passWord;
        this.driver="mySql";
        this.port="3306";
        this.serverIP="localhost";
        this.server=server;
    }
    public SqlBase(String userNmae,String passWord,String driver,String port,String serverIP,String server)
    {
        //初始化对象
        this.userNmae=userNmae;
        this.passWord=passWord;
        this.driver=driver;
        this.port=port;
        this.serverIP=serverIP;
        this.server=server;

    }

    public String getUserNmae() {
        return userNmae;
    }

    public void setUserNmae(String userNmae) {
        this.userNmae = userNmae;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public String getDriver() {
        return driver;
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getServerIP() {
        return serverIP;
    }

    public void setServerIP(String serverIP) {
        this.serverIP = serverIP;
    }

    public String getServer() {
        return server;
    }

    public void setServer(String server) {
        this.server = server;
    }

    public void initSqlBase() throws Exception {
        //1、加载驱动
        if (driver.equals("mySql")){
            Class.forName("com.mysql.jdbc.Driver");
        //2、建立连接
            String url = "jdbc:mysql://"+serverIP+":"+port+"/"+server;
            conn= DriverManager.getConnection(url,userNmae,passWord);
            if (conn!=null){
                System.out.println("连接到：ServerIP:"+serverIP+":"+port+"服务器实例："+server+"                 =success!!!");
            }
        }else
            if (driver.equals("oracle")){
                //1、加载驱动
                Class.forName("oracle.jdbc.driver.OracleDriver");
                //2、建立连接
                String url="jdbc:oracle:thin:@"+serverIP+":"+port+":"+server;
                conn= DriverManager.getConnection(url,userNmae,passWord);

            }else
                if (driver.equals("sqlServer")){
                       //不支持sqlServer
                    throw new Exception("不支持sqlserver");
                }else {
                    throw new Exception("driver参数错误，只能为mySql-oracle-sqlServer");
                }
    }

    public void  _add(String Table,String[] keys,String[] values) throws SQLException {
      String sql="insert into "+Table+"(";
      for (String key:keys){
          sql=sql+key;
          if (!key.equals(keys[keys.length-1])){
              sql=sql+",";
          }
      }
      sql=sql+") values(";
        for (String value:values){
            sql=sql+"'"+value+"'";
            if (!value.equals(values[values.length-1])){
                sql=sql+",";
            }
        }
        sql=sql+")";
        st=conn.createStatement();
        int num=st.executeUpdate(sql);
        if (num>0){
            System.out.println("添加数据成功");
        }else{
            throw  new SQLException("出现未知错误，数据添加失败");
        }



    }

    public void  _del(String Table,String key,String value) throws SQLException {
        String sql="delete from "+Table+" where "+key+"='"+value+"'";
        st=conn.createStatement();
        int num=st.executeUpdate(sql);
        if (num>0){
            System.out.println("删除数据成功");
        }

    }
    public String _sel(String Table,String cNmame,String key,String value) throws SQLException {

       // String sql="select empno,ename,job from emp";
       String sql="SELECT "+cNmame+" FROM "+Table+" WHERE "+key+"='"+value+"'";
        st=conn.createStatement();
        rs=st.executeQuery(sql);
        //4.处理结果集
        while(rs.next()){
            return rs.getString(1);
        }
        return "";
    }
    public void _update(String Table,String key,String value,String updateKey,String upDateValue) throws SQLException {
        String sql="update "+Table+" set "+updateKey+"='"+upDateValue+"' where "+key+"='"+value+"' ";
			st=conn.createStatement();
			int num=st.executeUpdate(sql);
			if(num>0){
				System.out.println("修改数据成功");
			}


    }
}